auto.waitFor("fast");
// setScreenMetrics(1280, 800);
log(device.width)
log(device.height)


//识别金币圣水数量ocr
function moneyOcr(){
    log("金币ocr开始识别")
    var img = captureScreen();
    var imgclip = images.clip(img,58,82,170,70);
    imgclip.saveTo("/sdcard/04.png");
    img.recycle();


    var url = "http://192.168.16.172:8080/aaa";
    var result = http.postMultipart(url,{
        file:open("/sdcard/04.png")
    })
    log(result)
    var json = result.body.json();
    log(json.core)
    log(json.mess)
    return json.mess;
}
//识别工人数量ocr
function workerOCR(){
    log("工人ocr开始识别")
    var img = captureScreen();
    var imgclip = images.clip(img,494,20,60,40);//556 57
    imgclip.saveTo("/sdcard/04.png");
    img.recycle();


    var url = "http://192.168.16.172:8080/aaa";
    var result = http.postMultipart(url,{
        file:open("/sdcard/04.png")
    })
    log(result)
    var json = result.body.json();
    log(json.core)
    log("mess=="+json.mess)
    return json.mess;
}

//main
function main(){
    var MIN = 3000;
    var r = random(0,5);
    var t = random(200,700);
    log("random="+r);
    log("time="+t);
    //双手捏合
    gestures([0, 500, [275, 387], [518, 421]],
            [0, 500, [1005, 382], [712, 275]]);

    // while(!click("商店"));

    log("打开建筑工人")
    sleep(500+t);
    click(460+r,30+r);//建筑工人

    log("点击左下角进攻按钮")
    sleep(500+t);
    click(77+r,723+r);//左下角进攻按钮

    log("点击搜索对手按钮")
    sleep(500+t);
    click(936+r,446+r);//左下角 搜索对手 按钮

    log("识别金币圣水情况")
    sleep(5000);
    var resource = moneyOcr();
    var res = resource.split(";");
    sleep(3000);
    var flag = (resource=="" || res[0]<MIN || res[1]<MIN);
    var i = 0;
    log("flag:"+flag)
    while(flag){
        log("金币圣水太少，搜下一个:"+i)
        click(1161+r,608+r);//下一个 按钮
        sleep(3000);
        resource = moneyOcr();
        res = resource.split(";");
        flag = (resource=="" || res[0]<MIN || res[1]<MIN);
        log("flag2:"+flag)
        sleep(3000);
        log(i);
        i=i+1
    }

    log("下兵")
    sleep(3000+t);
    click(187+r,739+r);//第一格兵种

    //缩小，拉右上方
    sleep(500+t);
    gestures([0, 500, [275, 387], [518, 421]],
        [0, 500, [1005, 382], [712, 275]]);
    sleep(200+t);
    swipe(853+r,250+r,660+r,460+r,200);

    //下兵
    for(var i=0;i<40;i++){
        sleep(50+r);
        click(675+r+i*5,130+r+i*2);
        //    679        132
    }

    //结束
    sleep(60*1000*3+t);
    log("回营")
    click(630+r,665+r);//回营 按钮
    sleep(10000);

    log("造兵")
    //造兵
    sleep(500+t);
    click(42+r,617+r);//训练营 按钮
    sleep(500+t);
    click(803+r,93+r);//一键训练 按钮
    sleep(500+t);
    click(803+r,93+r);//一键训练 按钮
    sleep(500+t);
    click(1071+r,382+r);//军队配置一 训练按钮
    sleep(500+t);
    click(1119+r,102+r);//训练营 关闭按钮//hk08机型坐标
    click(1192+r,75+r);//训练营 关闭按钮//t1机型坐标

    sleep(2000);
    log("判断建筑工人数量")
    var sum = workerOCR();
    if(sum!="" && sum[0]!="0"){
        log("打开建筑工人")
        sleep(500+t);
        click(460+r,30+r);//建筑工人

        sleep(500+t);
        click(489+r,359+r);//选项 对象

        sleep(500+t);
        click(705+r,644+r);//升级 按钮

        sleep(500+t);
        click(613+r,653+r);//选项 对象

    }

    sleep(3000);
}

/*
//请求截图权限
if (!requestScreenCapture(false)) {
    toast("请求截图失败");
    exit();
}
while(true){
    log("脚本开始")
    var coc = app.getPackageName("部落冲突");
    var flag = app.launch(coc);
    if(flag){log("succ");}
    else    {log("coc start failed");exit();}
    sleep(30000);

    main();

    home();
    sleep(300*1000);
}*/
